package com.xxx.notice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.entity.PageResult;
import com.xxx.entity.Result;
import com.xxx.entity.StatusCode;
import com.xxx.notice.domain.Article;
import com.xxx.notice.domain.Notice;
import com.xxx.notice.domain.NoticeFresh;
import com.xxx.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author blijojo
 * @date 2021/6/26 11:51
 */
@RestController
@RequestMapping("/notice")
@CrossOrigin
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 根据id查询消息通知
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        Notice notice = noticeService.findById(id);
        return new Result(StatusCode.OK, true, "查询通知成功", notice);
    }

    /**
     * 根据条件分页查询消息通知
     */
    @PostMapping("/search/{page}/{size}")
    public Result pageByCondition(@RequestBody Notice notice,
            @PathVariable("size") Integer size,
            @PathVariable("page") Integer page) {
        Page<Notice> pageData = noticeService.pageByCondition(notice, page, size);
        PageResult<Notice> pageResult = new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(StatusCode.OK, true, "查询通知成功", pageResult);
    }


    /**
     * 新增通知
     */
    @PostMapping()
    public Result save(@RequestBody Notice notice) {
        noticeService.add(notice);
        return new Result(StatusCode.OK, true, "新增通知成功", null);
    }

    /**
     * 新增系统全员通知
     */
    @PostMapping("/sys")
    public Result save(@RequestBody Article article) {
        noticeService.addSysNotice(article);
        return new Result(StatusCode.OK, true, "新增系统通知成功", null);
    }

    /**
     * 修改通知
     */
    @PutMapping()
    public Result updateById(@RequestBody Notice notice) {
        noticeService.updateById(notice);
        return new Result(StatusCode.OK, true, "修改通知成功", null);
    }

    /**
     * 根据用户id查询该用户的待推送消息（新消息）
     */
    @GetMapping("/fresh/{userId}/{page}/{size}")
    public Result findNoticeFreshByUserIdWithPage(@PathVariable("userId") String userId,
                                                  @PathVariable("page") Integer page,
                                                  @PathVariable("size") Integer size) {
        Page<NoticeFresh> pageData = noticeService.findNoticeFreshByUserIdWithPage(userId,page,size);
        PageResult<NoticeFresh> pageResult = new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(StatusCode.OK, true, "查询待推送通知成功", pageResult);
    }

    /**
     * 删除待推送消息（新消息）
     */
    @DeleteMapping("/fresh")
    public Result deleteFresh(@RequestBody NoticeFresh noticeFresh) {
        noticeService.deleteFresh(noticeFresh);
        return new Result(StatusCode.OK, true, "删除待推送通知成功", null);
    }

    /**
     * 根据id删除通知
     */
    @DeleteMapping("/{noticeId}")
    public Result deleteById(@PathVariable("noticeId") String noticeId) {
        noticeService.removeById(noticeId);
        return new Result(StatusCode.OK, true, "删除通知成功", null);
    }

    /**
     * 根据条件查询通知
     */
    @PostMapping("/search")
    public Result findByCondition(@RequestBody Map<String, Object> map) {
        Notice notice = noticeService.findByCondition(map);
        return new Result(StatusCode.OK, true, "根据条件查询通知成功", notice);
    }


}
